import unittest
from selenium import webdriver
import time,os,sys,csv
from ddt import ddt, unpack, data

def getTxt(file_name):
    # ([周迅,周迅_百度搜索],[张国荣,张国荣_百度搜索],[张一山，张一山_百度搜索])
    rows = []
    path = sys.path[0]
    with open(path+'/data/'+file_name, 'rt',encoding='utf-8') as f:
        readers = csv.reader(f, delimiter=',', quotechar='|')
        next(readers, None)
        for row in readers:
            temprows=[]
            for i in row:
                # 周迅,周迅_百度搜索
                temprows.append(i)
            rows.append(temprows)
        return rows

@ddt
class TestUnit2(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.url = "http://124.223.113.147:9191/login.html"
        self.driver.maximize_window()
        driver = self.driver
        url = self.url
        driver.get(url)
        driver.find_element_by_xpath("/html/body/div[2]/div[2]/form/div[1]/a").click()
        time.sleep(3)
        driver.find_element_by_name("Username").send_keys("admin")
        driver.find_element_by_name("password").send_keys("123456")
        time.sleep(2)
        driver.find_element_by_xpath("//*[@id='loginButton']").click()
        time.sleep(1)

    def tearDown(self):
        self.driver.quit()

    @data(*getTxt('uploadTest.txt'))
    @unpack
    def test_upload(self,musicpath,singer,srcpath):
        driver = self.driver
        driver.find_element_by_xpath("//*[@id='head']/div[2]/div[2]/a[2]").click()
        time.sleep(2)
        driver.find_element_by_name("filename").send_keys(os.path.abspath(musicpath))
        driver.find_element_by_name("singer").send_keys(singer)
        driver.find_element_by_name("imgname").send_keys(os.path.abspath(srcpath))
        time.sleep(2)
        driver.find_element_by_xpath("//*[@id='submit']").click()
        alert = driver.switch_to.alert
        self.assertEqual("上传成功",alert.text,msg="上传失败!")

    if __name__ == "__main__":
        unittest.main()